﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Web;

namespace WikcLib.Http
{
    public class Post
    {
        public string PostModel(string strUrl, string strParm,string strEncode)
        {
            //strParm = HttpUtility.UrlEncode(strParm);
            Encoding encode = System.Text.Encoding.GetEncoding(strEncode);
            byte[] arrB = encode.GetBytes(strParm);

            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
            myReq.Method = "POST";
            myReq.ContentType = "application/x-www-form-urlencoded";
            myReq.ContentLength = arrB.Length;
            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();
            WebResponse myResp = null;
            try
            {
                //接收HTTP做出的响应  
                myResp = myReq.GetResponse();
            }
            catch (Exception)
            {
                throw;
            }
            Stream ReceiveStream = myResp.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream, encode);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            string str = null;
            while (count > 0)
            {
                str += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            readStream.Close();
            myResp.Close();
            return str;
        }

        public string PostModel2(string strUrl, string strParm, string strEncode)
        {
            //strParm = HttpUtility.UrlEncode(strParm);
            Encoding encode = System.Text.Encoding.GetEncoding(strEncode);

            byte[] arrB = encode.GetBytes(strParm);

            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
            myReq.Method = "POST";
            myReq.ContentType = "application/x-www-form-urlencoded";
            myReq.ContentLength = arrB.Length;

            string error = "";
            StreamWriter myWriter = null; 
            myWriter = new StreamWriter(myReq.GetRequestStream());
            myWriter.Write(strParm);

            
            return ""; 
        }

    }
}
